// Copyright (c) 2011 Vratislav Cermak (vratacermak@gmail.com)
//  
// This file is part of SrtSync Project. See http://code.google.com/p/srtsync/ for more information.
//  
// SrtSync is an open source project. Using of SrtSync is free of charge.
// It is distributed under GNU General Public License version 3 (GPLv3).
// GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
//  
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.

using System;
using System.Drawing;
using System.IO;
using System.Xml.Serialization;
using SrtSync.Core.Framework;

namespace SrtSync.Core
{
    public class Settings : ISettings
    {
        private string _path;
        
        public string Folder { get; set; }

        public Point Location { get; set; }

        public Size Size { get; set; }

        public bool Maximized { get; set; }

        public PersistentEncoding LeftEncoding { get; set; }

        public PersistentEncoding RightEncoding { get; set; }
        
        public bool Statusbar { get; set; }

        public static Settings Load(string path, out bool firstRun)
        {
            firstRun = false;

            Settings userSettings = null;

            try
            {
                if (File.Exists(path))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                    using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
                    {
                        userSettings = serializer.Deserialize(stream) as Settings;
                    }
                }
            }
            catch (Exception)
            {
                //
            }

            if (userSettings == null)
            {
                firstRun = true;
                userSettings = new Settings();
            }

            if (userSettings.LeftEncoding == null)
            {
                userSettings.LeftEncoding = new PersistentEncoding();
            }

            if (userSettings.RightEncoding == null)
            {
                userSettings.RightEncoding = new PersistentEncoding();
            }

            userSettings._path = path;

            return userSettings;
        }

        public void Save()
        {
            string directory = Path.GetDirectoryName(_path);
            if (String.IsNullOrEmpty(directory))
            {
                throw new InvalidOperationException();
            }

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                using (FileStream stream = new FileStream(_path, FileMode.Create, FileAccess.Write))
                {
                    serializer.Serialize(stream, this);
                }
            }
            catch (Exception)
            {
                //
            }
        }
    }
}